package com.yhj.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yhj.reggie.dto.DishDto;
import com.yhj.reggie.entity.Category;
import com.yhj.reggie.entity.Dish;
import com.yhj.reggie.entity.DishFlavor;
import com.yhj.reggie.entity.Page;
import com.yhj.reggie.mapper.CategoryMapper;
import com.yhj.reggie.mapper.DishFlavorMapper;
import com.yhj.reggie.mapper.DishMapper;
import com.yhj.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements DishService {

    @Autowired(required = false)
    private DishMapper dishMapper;
    @Autowired(required = false)
    private DishFlavorMapper dishFlavorMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加菜品
     *
     * @param dishDto
     * @return
     */
    @Override
    public boolean save(DishDto dishDto) {
        //完善菜品数据
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setSort(0);
        dishDto.setStatus(1);

        dishMapper.save(dishDto);

        //完善味道数据

        List<DishFlavor> dishFlavorList = dishDto.getFlavors().stream().map((dishFlavor -> {
            dishFlavor.setDishId(dishDto.getId());
            dishFlavor.setUpdateTime(LocalDateTime.now());
            dishFlavor.setCreateTime(LocalDateTime.now());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            return dishFlavor;

        })).collect(Collectors.toList());

        dishFlavorMapper.save(dishFlavorList);
        //清除缓存
        redisTemplate.delete("dish_"+dishDto.getCategoryId()+"_1");

        return true;
    }

    /**
     * 分页查找菜品
     *
     * @param page     页码
     * @param pageSize 页面大小
     * @param name     模糊查找名字
     * @return 页面对象
     */
    @Override
    public Page<DishDto> findByPage1(Integer page, Integer pageSize, String name) {
       /* List<Dish> dishList = dishMapper.findAll1(name);
        System.out.println("=====封装前===="+dishList);
        return null;*/
        PageHelper.startPage(page,pageSize);
        List<Dish> dishList = dishMapper.findAll1(name);
        System.out.println("=====封装前===="+dishList);
        PageInfo<Dish> dishPageInfo = new PageInfo<>(dishList);
        System.out.println("=====封装后===="+dishList);

//        List<Dish> PageInfoDishList = dishPageInfo.getList();


        List<DishDto> dishDtoList = dishList.stream().map((dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);

            Category category = categoryMapper.findById(dishDto.getCategoryId());

            dishDto.setCategoryName(category.getName());
            return dishDto;
        })).collect(Collectors.toList());



        Page resultPage = new Page();
        resultPage.setRecords(dishDtoList);
        resultPage.setTotal(dishPageInfo.getTotal());

        return resultPage;
    }

    /**
     * 分页查找菜品
     *
     * @param page     页码
     * @param pageSize 页面大小
     * @param name     模糊查找名字
     * @return
     */
    @Override
    public Page<DishDto> findByPage2(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);
        List<DishDto> dishDtoList = dishMapper.findAll2(name);
        PageInfo<DishDto> dishDtoPageInfo = new PageInfo<>(dishDtoList);

        Page<DishDto> resultPage = new Page<>();
        resultPage.setRecords(dishDtoPageInfo.getList());
        resultPage.setTotal(dishDtoPageInfo.getTotal());
        return resultPage;
    }

    /**
     * 根据id找菜品和多个口味
     *
     * @param id
     * @return
     */
    @Override
    public DishDto findById(Long id) {
        DishDto dishDto = dishMapper.findById(id);
        return dishDto;
    }

    /**
     * 修改菜品
     *
     * @param dishDto
     */
    @Override
    @Transactional
    public void update(DishDto dishDto) {
        dishDto.setUpdateTime(LocalDateTime.now());
        dishMapper.update(dishDto);
        dishFlavorMapper.delete(dishDto.getId());

        List<DishFlavor> dishFlavorList = dishDto.getFlavors().stream().map((dishFlavor -> {
            dishFlavor.setDishId(dishDto.getId());
            dishFlavor.setUpdateTime(LocalDateTime.now());
            dishFlavor.setCreateTime(LocalDateTime.now());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            return dishFlavor;

        })).collect(Collectors.toList());

        dishFlavorMapper.save(dishFlavorList);

        Set keys = redisTemplate.keys("dish*");
        redisTemplate.delete(keys);
    }


    /**
     * 根据类别id(category)找菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<DishDto> findByCid(Long categoryId ,Integer status) {

        String key ="dish_" +categoryId+ "_" +status;
        List<DishDto> dishDtoList = (List<DishDto>) redisTemplate.opsForValue().get(key);

        if (dishDtoList==null) {
            List<Dish> dishList = dishMapper.findByCid(categoryId, status);
            dishDtoList = dishList.stream().map((dish -> {
                DishDto dishDto = new DishDto();
                BeanUtils.copyProperties(dish, dishDto);

                //获取菜品口味数据
                List<DishFlavor> dishFlavorList = dishFlavorMapper.findByDishId(dishDto.getId());
                dishDto.setFlavors(dishFlavorList);

                return dishDto;
            })).collect(Collectors.toList());
            redisTemplate.opsForValue().set(key,dishDtoList);
        }
        return dishDtoList;
    }
}
